package com.liuguang.framework.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.liuguang.framework.onActivityForResult.OnActivityForResultUtils;
import com.liuguang.framework.utils.AppUtils;
import com.liuguang.framework.utils.PermissionUtil;
import java.util.ArrayList;
import java.util.List;
@SuppressLint("WrongConstant")
public class BaseActivity extends AppCompatActivity {
    private final int MIN_CLICK_DELAY_TIME = 1000;
    private String TAG = "MyBaseFragmentActivity";
    private long lastClickTime;
    public BaseActivity mActivity;
    public Context mContext;
//    private CompositeDisposable mDisposable;
    public ArrayList<View> mObjects = new ArrayList<>();

    public void onHideSoftInput() {
    }

    /* access modifiers changed from: protected */

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setRequestedOrientation(1);
        AppUtils.getApplication().addActivity(this);
        this.mContext = this;
        this.mActivity = this;
    }

    /* access modifiers changed from: protected */
    public void onDestroy() {
        super.onDestroy();
        AppUtils.getApplication().removeActivity(this);
//        if (this.mDisposable != null) {
//            this.mDisposable.dispose();
//        }
    }

    /* access modifiers changed from: protected */
//    public void addDisposable(Disposable disposable) {
//        if (this.mDisposable == null) {
//            this.mDisposable = new CompositeDisposable();
//        }
//        this.mDisposable.add(disposable);
//    }

    public View getViewFromId(@LayoutRes int i, ViewGroup viewGroup) {
        return LayoutInflater.from(this).inflate(i, viewGroup, false);
    }

    @SuppressLint("WrongConstant")
    public void startVibrator() {
        ((Vibrator) getSystemService("vibrator")).vibrate(1000);
    }

    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        hideInputWhenTouchOtherView(this, motionEvent, this.mObjects);
        return super.dispatchTouchEvent(motionEvent);
    }

    public void hideInputWhenTouchOtherView(Activity activity, MotionEvent motionEvent, List<View> list) {
        if (motionEvent.getAction() == 0) {
            if (list != null && !list.isEmpty()) {
                int i = 0;
                while (i < list.size()) {
                    if (!isTouchView((View) list.get(i), motionEvent)) {
                        i++;
                    } else {
                        return;
                    }
                }
            }
            View currentFocus = activity.getCurrentFocus();
            if (isShouldHideInput(currentFocus, motionEvent)) {
                onHideSoftInput();
                InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService("input_method");
                if (inputMethodManager != null) {
                    inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
                }
            }
        }
    }

    public boolean isTouchView(View view, MotionEvent motionEvent) {
        if (view == null || motionEvent == null) {
            return false;
        }
        int[] iArr = {0, 0};
        view.getLocationInWindow(iArr);
        int i = iArr[0];
        int i2 = iArr[1];
        int height = view.getHeight() + i2;
        int width = view.getWidth() + i;
        if (motionEvent.getRawX() <= ((float) i) || motionEvent.getRawX() >= ((float) width) || motionEvent.getRawY() <= ((float) i2) || motionEvent.getRawY() >= ((float) height)) {
            return false;
        }
        return true;
    }

    public boolean isShouldHideInput(View view, MotionEvent motionEvent) {
        if (view == null || !(view instanceof EditText)) {
            return false;
        }
        return !isTouchView(view, motionEvent);
    }

    public void hideKeyboard(IBinder iBinder) {
        if (iBinder != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService("input_method");
            if (inputMethodManager != null) {
                inputMethodManager.hideSoftInputFromWindow(iBinder, 2);
            }
        }
    }

    public void openKeyboard(EditText editText) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService("input_method");
        if (inputMethodManager != null) {
            inputMethodManager.showSoftInput(editText, 1);
        }
    }

    /* access modifiers changed from: protected */
    @SuppressLint({"RestrictedApi"})
    public void onActivityResult(int i, int i2, Intent intent) {
        OnActivityForResultUtils.onActivityResult(i, i2, intent);
        super.onActivityResult(i, i2, intent);
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        for (int i3 = 0; i3 < supportFragmentManager.getFragments().size(); i3++) {
            Fragment fragment = (Fragment) supportFragmentManager.getFragments().get(i3);
            if (fragment == null) {
                String str = this.TAG;
                StringBuilder sb = new StringBuilder();
                sb.append("Activity result no fragment exists for index: 0x");
                sb.append(Integer.toHexString(i));
                Log.d(str, sb.toString());
            } else {
                try {
                    handleResult(fragment, i, i2, intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void onRequestPermissionsResult(int i, @NonNull String[] strArr, @NonNull int[] iArr) {
        super.onRequestPermissionsResult(i, strArr, iArr);
        PermissionUtil.onRequestPermissionsResult(i, strArr, iArr);
    }

    private void handleResult(Fragment fragment, int i, int i2, Intent intent) {
        fragment.onActivityResult(i, i2, intent);
        Log.e(this.TAG, "MyBaseFragmentActivity");
        List<Fragment> fragments = fragment.getChildFragmentManager().getFragments();
        if (fragments != null) {
            for (Fragment fragment2 : fragments) {
                if (fragment2 != null) {
                    handleResult(fragment2, i, i2, intent);
                }
            }
        }
        if (fragments == null) {
            Log.e(this.TAG, "MyBaseFragmentActivity1111");
        }
    }

    public boolean isForeground() {
        if (TextUtils.isEmpty(getLocalClassName())) {
            return false;
        }
        List runningTasks = ((ActivityManager) getSystemService("activity")).getRunningTasks(1);
        if (runningTasks != null && runningTasks.size() > 0) {
            if (getLocalClassName().equals(((RunningTaskInfo) runningTasks.get(0)).topActivity.getClassName())) {
                return true;
            }
        }
        return false;
    }

    public boolean isFastClick() {
        long currentTimeMillis = System.currentTimeMillis();
        boolean z = currentTimeMillis - this.lastClickTime >= 1000;
        this.lastClickTime = currentTimeMillis;
        return z;
    }

    public int getStatusBarHeight() {
        int identifier = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifier > 0) {
            return getResources().getDimensionPixelSize(identifier);
        }
        return 0;
    }
}
